package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.Daily;
import cn.wolfcode.trip.base.domain.DailyCommentReply;
import cn.wolfcode.trip.base.domain.DailyReview;
import cn.wolfcode.trip.base.query.DailyQueryObject;
import cn.wolfcode.trip.base.service.IDailyService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("daily")
public class DailyController {
    @Autowired
    private IDailyService dailyService;

    @RequestMapping(value = "list",method = RequestMethod.GET)
    public PageInfo listStrategies(@ModelAttribute("qo") DailyQueryObject qo){
        qo.setOrderBy("dailyCreateTime desc");
        return dailyService.query(qo);
    }
    @GetMapping("/{id}")
    public Daily getDailyById(@PathVariable Long id){
        Daily daily=dailyService.getById(id);
        return daily;
    }
    @GetMapping("/{dailyId}/comments")
    public PageInfo getDailyReviewsById(DailyQueryObject qo){
        qo.setOrderBy("dr.review_time desc");
        return  dailyService.getDailyReviewsById(qo);
    }

    /**
     * 2.保存用户提交的游记评论
     *
     * @param qo
     * @return
     */
    @PostMapping("/{dailyId}/comments")
    public JSONResult saveComments(DailyQueryObject qo) {
        DailyReview dailyReview = new DailyReview();
        Daily daily = new Daily();
        dailyReview.setDaily(daily);
        dailyReview.getDaily().setId(qo.getDailyId());
        dailyReview.setDailyReviews(qo.getDailyReviews());
        JSONResult jsonResult = new JSONResult();
        try {
            dailyReview.setUser(UserContext.getCurrentUser());
            dailyService.saveOrUpdateReview(dailyReview);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("保存失败");
        }
        return jsonResult;
    }
    //保存日报评论回复
    @PostMapping("/comments/{commentId}")
    public JSONResult saveReply(DailyCommentReply commentReply){
        commentReply.setUser(UserContext.getCurrentUser());
        dailyService.saveReply(commentReply);
        return new JSONResult();
    }

}